<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Ttc\Model;
use Think\Model;

/**
 * 用户模型
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */

class TtcGoodsModel extends Model {

    public $sx_data = array();

    protected $_validate = array(
        // array('goods_storage','is_emptyStorage','请输入正确的库存',1,'function'),//库存不为0
        array('goods_name','','请输入商品名称',1,'notequal'),//商品名称不为空
        array('goods_price','','请输入商品价格',1,'notequal'),//商品价格不为空
        array('images_id','','至少上传一张图片',1,'notequal'),//商品价格不为空
        array('contact_id','','请添加商品地址',1,'notequal'),//商品价格不为空
        array('goods_add_place','','没有获取到当前位置',1,'notequal'),//商品价格不为空
        array('goods_deliver','','请选择发货方式',1,'notequal'),//发货方式不为空
        array('goods_type','','请选择产品类型',1,'notequal'),//产品类型不为空
        
        // array('goods_deliver','/^[0-9]{1,}$/','请选择发货方式',1),//发货方式是数字
        // array('goods_type','/^[0-9]{1,}$/','请选择产品类型',1),//产品类型是数字

    );

    /**
     * 添加土特产
     */
    public function addGoods(){
        $res = $this->create($this->sx_data);
        if(!$res){
            $res['info'] = $this->getError();
            $res['msg'] = false;
            return $res;
        }else{
            $res = $this->add();
            return $res;
        }
    }
    /**
     * 土特产详情
     * @param  integer $data 土特产信息数组
     */
    public function GoodsInfo($data){
        // $wh['member_id'] = $data['member_id'];
        $wh['id'] = $data;
        $res = $this->where($wh)->find();
        if($res){
            // var_dump($res);
            // var_dump($res['goods_add_time']);
            $res['goods_add_time'] = date('Y-m-d',$res['goods_add_time']);
            $res['goods_expire_time'] = date('Y-m-d',$res['goods_expire_time']);
            //发货方式中文
            $deliver_id = explode(',', $res['goods_deliver']);
            // var_dump($deliver_id);
            $goods_deliver_desc = '';

            foreach ($deliver_id as $k => $v) {
               // var_dump($v);
               if($v == 0){
                $goods_deliver_desc .= '快递/';
               }
               if($v == 1){
                $goods_deliver_desc .= '自提/';
               }
            }
            $res['goods_deliver_desc'] = rtrim($goods_deliver_desc,'/');
            //产品类型中文
            $type_id = explode(',', $res['goods_type']);
            // var_dump($deliver_id);
            $goods_type_desc = '';

            foreach ($type_id as $k => $v) {
               // var_dump($v);
               if($v == 0){
                $goods_type_desc .= '现货/';
               }
               if($v == 1){
                $goods_type_desc .= '预订/';
               }
            }
            $res['goods_type_desc'] = rtrim($goods_type_desc,'/');

            //查产品地址（详细地址）
            $Contact = D('Contact');
            $condition = array('id'=>$res['contact_id']);
            $field = 'contact_name,contact_phone,contact_wechat,areaid_1,areaid_2,areaid_3,longitude,latitude,detailed_address';
            $pro_addr = $Contact->getOneContact($condition,$field);
            //产品地址经纬度
            $res['pro_lt']['longitude'] = $pro_addr['longitude'];
            $res['pro_lt']['latitude'] = $pro_addr['latitude'];

            //发布地址经纬度
            $res['add_lt']['longitude'] = $res['longitude'];
            $res['add_lt']['latitude'] = $res['latitude'];

            $res['pro_addr'] = $pro_addr['areaid_1'].' '.$pro_addr['areaid_2'].' '.
            $pro_addr['areaid_3'].' '.$pro_addr['detailed_address'];
            $res['contact_name'] = $pro_addr['contact_name'];

            //查图片
            if(!empty($res['images_id'])){
                $imgs_id = explode(',', $res['images_id']);
                // var_dump($imgs_id);
                $res['goods_img'] = array();
                $Picture = D('Picture');
                foreach ($imgs_id as $k => $v) {
                    // var_dump($v);
                    // 查图片
                    // $wh['id'] = $v;
                    $img_res = $Picture->ImgInfo($v);
                    // var_dump($res);
                    $res['goods_img'][] = $img_res;
                }
            }else{
                $res['goods_img'] = [];
            }
            // var_dump($res);
            return $res;
        }

        return $res;

    }
    /**
     * 土特产列表
     */
    public function GoodsLists($data,$share=0){
        // var_dump($data);
        //用户id，改成session拿
        //本商家操作要显示所有商品，分享进来的话不要显示审核失败或未审核的商品
        if($share === 1){
            $wh['is_examine'] = ['LT',2];
        }
        $wh['member_id'] = $data['member_id'];

        $page = $data['page'];//第几页
        $rows = $data['rows'];//1页多少行

        //总条数
        $num = $this->field('count(*)')
        ->where($wh)->find();
        // var_dump($num['count(*)']);
        $all_page = ceil($num['count(*)']/$rows);
        // var_dump($all_page);

        $limit = ($data['page'] - 1)*$data['rows'];

        $res['data'] = $this->field('id,goods_name,goods_price,images_id,contact_id,is_examine')
        ->order('goods_add_time desc,id desc')
        ->limit($limit.','.$rows)
        ->where($wh)->select();

        foreach ($res['data'] as $k => $v) {
            //查距离
            // var_dump($v['contact_id']);
            $Contact = D('Contact');
            $lres = $Contact->getOneContact(array('id'=>$v['contact_id']),'longitude,latitude');
            $dt = getDistanceBetweenPointsNew($lres['latitude'],$lres['longitude'],$data['lati'],$data['long'],false);
            // var_dump($dt);
            if($dt['meters'] > 1000){
                $dt['meters'] = sprintf('%.1f',$dt['meters']/1000);
                $distance = $dt['meters'].'km';
            }else{
                $dt['meters'] = sprintf('%.1f',$dt['meters']);
                $distance = $dt['meters'].'m';
            }
            $res['data'][$k]['distance'] = $distance;

            //查图片
            $img_id = explode(',', $v['images_id']);
            $Picture = D('Picture');
            $img_res = $Picture->ImgInfo($img_id[0]);
            $res['data'][$k]['img'] = $img_res;

            //查二级地区
            // var_dump($v['contact_id']);//地址id
            $cres = D('Contact')->find($v['contact_id']);
            $res['data'][$k]['addr'] = $cres['areaid_2'];//二级地区

        }
        $res['all_page'] = $all_page;
        // var_dump($res);
        return $res;

    }

    /**
     * 查询单条商品信息
     */
    public function getOneGoods($condition, $field = '*') {
        return $this->field($field)->where($condition)->find();
    }

     /**
     * 修改土特产转发次数
     */
    public function saveGoodsForw($goods_id){
        $res = $this->where('id = '.$goods_id)->setInc('forward_amount',1);       
        return $res;
    }

    /**
     * 修改土特产联系次数
     */
    public function saveGoodsClick($goods_id){
        $res = $this->where('id = '.$goods_id)->setInc('goods_click',1);       
        return $res;
    }

    /**
     * 修改商品
     */
    public function saveGoods($condition,$data){
        $res = $this->where($condition)->save($data);       
        return $res;
    }
    

}
